﻿using Newtonsoft.Json;
using NLog.Fluent;
using SHDM.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;

namespace SHDM.Web.Infrastructure
{
    public class ExceptionResult : IHttpActionResult
    {
        Exception Ex
        {
            get; set;
        }

        public ExceptionResult(Exception exception)
        {
            NLog.LogManager.GetLogger("ExceptionResult").Error(exception.ToString());
            this.Ex = exception;
        }
        public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
        {
            var response = new HttpResponseMessage(HttpStatusCode.BadRequest);
            var result = new Result() { ErrorCode = 1, ErrorMessage = string.Format("系统错误:{0}", Ex.Message) };
            response.Content = new StringContent(JsonConvert.SerializeObject(result), Encoding.UTF8, "application/json");
            return Task.FromResult(response);
        }
    }
}